using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace OsdmSoftware.CommonLibrary
{
    public class FileDialogFolderPreview: OwnerDrawnListBox, IPreviewControl
    {
        CompleteFileDialog dialog;
        public FileDialogFolderPreview()
        {
        }

        public void DockToDialog(Panel panel, CompleteFileDialog dialog)
        {
            this.dialog = dialog;
            this.ItemHeight = dialog.ImageList.ImageSize.Height;
            panel.Controls.Add(this);
            this.Dock = DockStyle.Fill;
        }

        public void SelectedFileChanged()
        {
            if (!String.IsNullOrEmpty(dialog.FileName))
            {
                CompleteFileDialog.ReadDirectoryContent(dialog.FileName, this, false, false, null, new string[] { "*" });
            }
            else
            {
                this.Items.Clear();
                this.UpdateItems();
                this.Refresh();
            }
        }

        public void UndockFromDialog()
        {
            Parent.Controls.Remove(this);
            dialog = null;
        }

        protected override void OnDrawItem(Graphics gr, object item, int index, int x, int y, bool selected)
        {
            CompleteFileDialog.FileItem fi = (CompleteFileDialog.FileItem) item;
            Image im = null;
            if (fi.ImageIndex == 0)
                im = dialog.ImageList.Images[fi.ImageIndex];
            DrawBitmapAndText(gr, im, fi.FileName, x, y, selected, 1, Color.White,
                this.ForeColor, this.ClientSize.Width);
        }
    }
}